const Router = require('koa-router');
const user = new Router();


//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
    database: 'running',
    user: 'run',
    port: 5432,
    host: '82.157.94.115',
    password: 'z12345678',
    max: 2
})

function makeId() {
    let id = Math.random().toString(16).substring(2);
    let id2 = Math.random().toString(16).substring(2);
    return (id + id2).substring(0, 12);
}

user.post('/user', async ctx => {
    let {user_sex,user_age } = ctx.request.body;
    console.log('user_sex',user_sex)
    console.log('user_age',user_age)
    
    let insertPostgresql = `insert into users (user_id,user_sex,user_age) values ($1, $2, $3)`

    let result = await psql.query(insertPostgresql, [makeId(), `${user_sex}`,user_age]);
    if (result.rowCount > 0) {
        ctx.body = {
            //user_id:user_id,
            user_sex:user_sex,
            user_age:user_age
        }
    }







}




)

module.exports =user;

